import BoundaryBuildInfo from "./boundary-build-info";
import Boundary from "./boundary";

export default class BoundaryBuilder {
    constructor(maxRows) {
        this.maxRows = maxRows;
        this.numBoundaries = 0;
        this.boundaries = new Array(1 << maxRows);
        this.bbInfo = new BoundaryBuildInfo(maxRows);
    }
    buildBoundariesRecursive(info,i) {
        if(i === info.numRows) {
            if(this.boundaries[this.numBoundaries] == null) {
                this.boundaries[this.numBoundaries] = new Boundary(this.maxRows);
            }
            this.boundaries[this.numBoundaries++].init(this.bbInfo);
            return;
        }
        let row = info.rows[i];
        let lowerLimitEnabled = row.minImpulse > -1e65536;
        let upperLimitEnabled = row.maxImpulse < 1e65536;
        if(row.minImpulse === 0 && row.maxImpulse === 0) {
            let _this = this.bbInfo;
            _this.iBounded[_this.numBounded] = i;
            _this.signs[_this.numBounded] = 0;
            _this.numBounded++;
            this.buildBoundariesRecursive(info,i + 1);
            this.bbInfo.numBounded--;
            return;
        }
        let _this = this.bbInfo;
        _this.iUnbounded[_this.numUnbounded] = i;
        _this.numUnbounded++;
        this.buildBoundariesRecursive(info,i + 1);
        this.bbInfo.numUnbounded--;
        if(lowerLimitEnabled) {
            let _this = this.bbInfo;
            _this.iBounded[_this.numBounded] = i;
            _this.signs[_this.numBounded] = -1;
            _this.numBounded++;
            this.buildBoundariesRecursive(info,i + 1);
            this.bbInfo.numBounded--;
        }
        if(upperLimitEnabled) {
            let _this = this.bbInfo;
            _this.iBounded[_this.numBounded] = i;
            _this.signs[_this.numBounded] = 1;
            _this.numBounded++;
            this.buildBoundariesRecursive(info,i + 1);
            this.bbInfo.numBounded--;
        }
    }
    buildBoundaries(info) {
        this.numBoundaries = 0;
        let _this = this.bbInfo;
        _this.numBounded = 0;
        _this.numUnbounded = 0;
        this.buildBoundariesRecursive(info,0);
    }
}